package com.gz.im.backend.config;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import com.corundumstudio.socketio.store.pubsub.PubSubStore;
import com.gz.im.backend.exception.InstantMessageExceptionListener;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author GuoZhong
 * @description netty socketIo配置类
 * @date 2023/3/26 22:31
 */
@Configuration
@Slf4j
public class NettySocketIoConfig {

	@Value("${socketIo.host}")
	private String host;

	@Value("${socketIo.port}")
	private Integer port;

	@Value("${socketIo.bossCount}")
	private int bossCount;

	@Value("${socketIo.workCount}")
	private int workCount;

	@Value("${socketIo.allowCustomRequests}")
	private boolean allowCustomRequests;

	@Value("${socketIo.upgradeTimeout}")
	private int upgradeTimeout;

	@Value("${socketIo.pingTimeout}")
	private int pingTimeout;

	@Value("${socketIo.pingInterval}")
	private int pingInterval;

	@Value("${socketIo.maxFramePayloadLength}")
	private int maxFramePayloadLength;

	@Value("${socketIo.maxHttpContentLength}")
	private int maxHttpContentLength;



	@Bean
	public SocketIOServer socketIOServer() {

		com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
		// 开启Socket端口复用 解决对此重启服务时，netty端口被占用问题
		com.corundumstudio.socketio.SocketConfig socketConfig = new com.corundumstudio.socketio.SocketConfig();
		socketConfig.setReuseAddress(true);
		socketConfig.setTcpNoDelay(true);
		socketConfig.setSoLinger(0);
		config.setSocketConfig(socketConfig);
		//主机地址
		config.setHostname(host);
		//端口
		config.setPort(port);
		//老板线程
		config.setBossThreads(bossCount);
		//工作线程
		config.setWorkerThreads(workCount);
		//允许为不同于 socket.io 协议的自定义请求提供服务
		config.setAllowCustomRequests(allowCustomRequests);
		//异常监听
		config.setExceptionListener(new InstantMessageExceptionListener());
		config.setUpgradeTimeout(upgradeTimeout);
		config.setPingTimeout(pingTimeout);
		config.setPingInterval(pingInterval);
		config.setMaxHttpContentLength(maxHttpContentLength);
		config.setMaxFramePayloadLength(maxFramePayloadLength);
		return  new SocketIOServer(config);
	}
    /**
     * 扫描socket 注解类
     */
	@Bean
	public SpringAnnotationScanner springAnnotationScanner() {
		return new SpringAnnotationScanner(socketIOServer());
	}


}
